<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理-登陆</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {
            background-image: url("../images/bg.jpg");
            height: 100%;
            width: 100%;
        }

        #container {
            height: 100%;
            width: 100%;
        }

        input:-webkit-autofill {
            -webkit-box-shadow: inset 0 0 0 1000px #fff;
            background-color: transparent;
        }

        .admin-login-background {
            width: 500px;
            height: 400px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            background-color: #fff;
            text-align: center;
        }

        .admin-header {
            text-align: center;
            margin-bottom: 20px;
            color: #ffffff;
            font-weight: bold;
            font-size: 40px
        }

        .admin-input {
            border-top-style: none;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            height: 50px;
            width: 300px;
            padding-bottom: 0px;
        }

        .admin-input::-webkit-input-placeholder {
            color: #a78369
        }

        .layui-icon-username {
            color: #a78369 !important;
        }

        .layui-icon-username:hover {
            color: #9dadce !important;
        }

        .layui-icon-password {
            color: #a78369 !important;
        }

        .layui-icon-password:hover {
            color: #9dadce !important;
        }

        .admin-input-username {
            border-top-style: solid;
            border-radius: 10px 10px 0 0;
        }

        .admin-input-verify {
            border-radius: 0 0 10px 10px;
        }

        .admin-button {
            margin-top: 20px;
            font-weight: bold;
            font-size: 18px;
            width: 300px;
            height: 50px;
            border-radius: 5px;
            background-color: #a78369;
            border: 1px solid #d8b29f
        }

        .admin-icon {
            margin-top: 10px;
            font-size: 30px;
            left: 360px;
        }

        i {
            position: absolute;
        }

        .admin-captcha {
            position: absolute;
            margin-left: -35px;
            margin-top: -48px;
        }
        .layui-form > div {
            margin-left: 100px;
        }
        .login-content {
            display: none;
        }
        .login-content.current {
            display: block;
        }
        .login-type {
            display: flex;
            justify-content: center;
        }
        .login-type > p {
            display: inline-flex;
            padding: 10px;
            font-size: 18px;
        }
        .login-content > h2 {
            padding: 10px;
        }
        .current-login-type a {
            color: #b91818;
        }
        #wx-message.success {
            color: green;
        }
        #wx-message > img {
            display: none;
        }
        #wx-message.success > img {
            display: inline-block;
        }
        #wx-login-img {
            margin-top: 20px;
            margin-bottom: 10px;
            height: 200px;
            width: 200px;
        }
        #refresh {
            height: 20px;
            position: absolute;
            top: 45%;
            right: 20%;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div id="container layui-anim layui-anim-upbit">
    <div></div>
    <div class="admin-login-background">
        <div class="admin-header">
            <span></span>
        </div>
        <div style="height: 300px;">
            <div class="login-content" id="wx-login">
                <h2>微信登录</h2>
                <div>
                    <img id="wx-login-img"/>
                    <img id="refresh" src="/images/refresh.jpeg" onclick="refreshWxacode()" width="20" alt="刷新" title="刷新小程序码"/>
                    <p id="wx-message">
                        <span></span><img src="/images/r.jpeg" width="20" alt=""/>
                    </p>
                </div>
            </div>
            <div class="login-content current" id="username-login">
                <h2>账号密码登录</h2>
                <form class="layui-form" action="">
                    <div>
                        <i class="layui-icon layui-icon-username admin-icon"></i>
                        <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input admin-input admin-input-username">
                    </div>
                    <div>
                        <i class="layui-icon layui-icon-password admin-icon"></i>
                        <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input admin-input">
                    </div>
                    <div>
                        <input id="captchaInput" type="text" name="captcha" placeholder="请输入验证码" autocomplete="off" class="layui-input admin-input admin-input-verify">
                        <img id="captcha" class="admin-captcha" width="135" height="45" src="/login/captcha" onclick="clearCaptcha();">
                    </div>
                    <button class="layui-btn admin-button" lay-submit="" lay-filter="login">登 陆</button>
                </form>
            </div>
        </div>
        <div style="margin-top: 20px;" class="login-type">
            <p class="current-login-type">
                <a href="javascript:" onclick="usernameLogin()">账号密码登录</a>
            </p>
            <p>
                <a href="javascript:" onclick="wxLogin()">微信登录</a>
            </p>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/lib/layui-v2.5.5/lay/modules/jquery.js"></script>
<script src="/js/token.js"></script>
<script src="/js/ws.js"></script>
<script>
    let sessionId;
    function wxLogin() {
        $('.login-content').removeClass('current');
        $('#wx-login').addClass('current');
        $('#wx-login-img').attr('src', '');
        let $message = $('#wx-message');
        $message.removeClass('success');
        $message.find('span').text('请使用微信扫码登录');
        if (createWs()) {
            onWsMessage((action, result) => {
                if (action === 'getId') {
                    sessionId = result.sessionId;
                    refreshWxacode();
                } else if (action === 'scan') {
                    $message.find('span').text('扫码成功，请在微信小程序中操作');
                    $message.addClass('success');
                } else if (action === 'login') {
                    $.post("/login", {'code': result.data, 'authType': 'wechat'}, res => {
                        if (res.code === 100) {
                            setToken(res.data);
                            window.location = '/';
                        }
                    });
                }
            });
        }
        sendWs({'action': 'getId'})
    }

    function refreshWxacode() {
        $('#wx-login-img').attr('src', '');
        $('#wx-login-img').attr('src', '/login/wechatLogin?t=' + new Date().getTime() + "&id=" + sessionId);
    }

    function usernameLogin() {
        $('.login-content').removeClass('current');
        $('#username-login').addClass('current');
    }
    layui.use(['form', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer;
        window.jQuery = window.$ = layui.jquery;
        $('.login-type a').click(function () {
            let parent = $(this).parent();
            parent.siblings().removeClass('current-login-type');
            parent.addClass('current-login-type');
        });

        // 登录过期的时候，跳出ifram框架
        if (top.location != self.location) top.location = self.location;

        // 进行登录操作
        form.on('submit(login)', function (data) {
            data = data.field;
            let { username, password, captcha } = data;
            if (!username) {
                layer.msg('用户名不能为空');
                return false;
            }
            if (!password) {
                layer.msg('密码不能为空');
                return false;
            }
            if (!captcha) {
                layer.msg('验证码不能为空');
                return false;
            }
            let deferred = $.Deferred();
            $.post("/login/captcha", {captcha})
            .done(data => {
                if (data.code === 100) {
                    deferred.resolve();
                } else {
                    layer.msg('验证码错误');
                    clearCaptcha();
                }
            })
            deferred.done(() => {
                $.post('/login', {username, password})
                .done(result => {
                    if (result.code === 100) {
                        setToken(result.data);
                        window.location = '/';
                    } else {
                        clearCaptcha();
                    }
                }).fail(clearCaptcha)
            })
            return false;
        });
    });

    function clearCaptcha() {
        $('#captcha')[0].src = '/login/captcha?t=' + new Date().getTime();
        $('#captchaInput').val('');
    }
</script>
</body>
</html>
